package com.schuxl.htmlobserver.observer;

import java.util.ArrayList;
import java.util.List;

import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import util.Util;

import com.schuxl.htmlobserver.dataobject.TausendPsAd;
import com.schuxl.htmlobserver.dataobject.schemaTypen.sites.Sites.Site;

public class TausendPsObserver extends HtmlObserver<TausendPsAd> {

	
	private static final String TAUSENDPS_LINK_URL = "http://www.1000ps.at";
	private static final String TAUSENDPS_IMAGE_URL = "http://images.1000ps.net/images/gebrauchtmotorrad/g_G";
	
	
	public TausendPsObserver(Site site) {
		super(site);
	}

	
	

	
	@Override
	protected List<TausendPsAd> getCurrentList(Document document) {
		List<TausendPsAd> currentList = new ArrayList<TausendPsAd>();
		TausendPsAd tausendPsAd = null;
		
		Elements elements = document.select("a[class=premiumpic]");
		
		for(Element element : elements) {
			tausendPsAd = new TausendPsAd();
			
			String title = 	element.attr("title");
			title = title.substring(18);
			tausendPsAd.setTitle(title);
			String link = element.attr("href");
			tausendPsAd.setLink(TAUSENDPS_LINK_URL+link.substring(0, link.indexOf("?")));
			
			//Unterscheidung zw. Standard- u. Premiumanzeige
			boolean premium = Util.isGoodString(element.select("div.premiumpicture").select("img").attr("src"));
			
			if(premium) { //Premium Anzeige
				tausendPsAd.setImageLink(element.select("div.premiumpicture").select("img").attr("src"));
				String premiumText = element.select("div.premium-tech-data").text();
				int yearStartIndex =  premiumText.indexOf("EZ: ")+4;
				tausendPsAd.setYear(Integer.parseInt(premiumText.substring(yearStartIndex, yearStartIndex+4).replaceAll("\\D+","")));
				int kmEndIndex =  premiumText.indexOf(" km");
				if(kmEndIndex<0)
					tausendPsAd.setKilometers("0");
				else {
					tausendPsAd.setKilometers(premiumText.substring(0,kmEndIndex).replaceAll("\\D+",""));
				}
				tausendPsAd.setPrice(Integer.parseInt(element.select("div.premiumprice").text().replaceAll("\\D+","")));
				String premiumDealerLocation =  element.select("div.premiumdealer").text();
				tausendPsAd.setDealer(premiumDealerLocation.contains("Privat")?"Privat":"Haendler");
				int premiumLocationStartIndex = premiumDealerLocation.indexOf("aus ");
				tausendPsAd.setLocation(premiumDealerLocation.substring(premiumLocationStartIndex+4).trim());
			}else { //NON-Premium Anzeige
				tausendPsAd.setImageLink("http:"+element.select("div.image-data").select("img").attr("src"));
				String premiumText = element.select("div.techdata").text();
				int yearStartIndex =  premiumText.indexOf("EZ: ")+4;
				tausendPsAd.setYear(Integer.parseInt(premiumText.substring(yearStartIndex, yearStartIndex+4).replaceAll("\\D+","")));
				int kmStartIndex =  premiumText.indexOf("km: ");
				if(kmStartIndex<0)
					tausendPsAd.setKilometers("0");
				else {
					kmStartIndex=kmStartIndex+4;
					int kmEndIndex = premiumText.indexOf(" ", kmStartIndex+1);
					tausendPsAd.setKilometers(premiumText.substring(kmStartIndex,kmEndIndex).replaceAll("\\D+",""));
				}
				tausendPsAd.setPrice(Integer.parseInt(element.select("div.preis").text().replaceAll("\\D+","")));
				tausendPsAd.setDealer(element.select("div.anbieter").text().contains("privat")?"Privat":"Haendler");
				tausendPsAd.setLocation(element.select("div.region").text());
			}
			
			currentList.add(tausendPsAd);
		}
		return currentList;
	}
			
				
	

	
	


}
